package cn.game.tanke.role;

import cn.game.tanke.panel.TankPanel;

import javax.swing.*;
import java.awt.*;

public class Boss extends Role {
    private static Image image = null;
    private static Boss boss = null;

    // 每帧高度
    private static final int FRAME_HEIGHT = 34;
    private static final int TOTAL_FRAMES = 12;

    // Boss生命值
    private int health = 10;
    private boolean isAlive = true;

    static {
        image = Toolkit.getDefaultToolkit().getImage(Boss.class.getClassLoader().getResource("images/boss.gif"));

        // 开启动画线程
        Runnable runnable = () -> {
            while (true) {
                try {
                    if (boss != null && boss.isAlive) {
                        boss.annimal();
                    }
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                } catch (Exception e) {
                    System.err.println("An unexpected error occurred in the Boss animation thread: " + e.getMessage());
                }
            }
        };
        new Thread(runnable).start();
    }

    private Boss() {
        super(image, 41, FRAME_HEIGHT, 0, 0, 14, 19); // 初始化图像和帧信息
    }

    public static Boss getInstance() {
        if (boss == null) {
            synchronized (Boss.class) {
                if (boss == null) {
                    boss = new Boss();
                }
            }
        }
        return boss;
    }

    @Override
    void move() {
        // 暂未实现移动逻辑
    }

    @Override
    void annimal() {
        int dir = getDir();
        if (dir >= TOTAL_FRAMES) {
            setDir(0);
        } else {
            setDir(dir + 1);
        }
    }

    @Override
    public void drawRole(Graphics graphics, JPanel jPanel) {
        if (!isAlive) return;

        int x = getX();
        int y = getY();
        int dir = getDir();

        // 确保不会越界
        if (dir >= TOTAL_FRAMES) dir = 0;

        // 绘制当前帧（整图中的一小块）
        graphics.drawImage(
                image,
                x * 32, y * 32, (x + 3) * 32, (y + 3) * 32, // 绘制区域（缩放后）
                0, dir * FRAME_HEIGHT, // 裁剪区域（源图像中的位置）
                41, (dir + 1) * FRAME_HEIGHT, // 裁剪区域（宽高）
                jPanel
        );

        // 绘制生命值条
        drawHealthBar(graphics, x * 32, y * 32 - 10);
    }

    private void drawHealthBar(Graphics g, int x, int y) {
        g.setColor(Color.RED);
        g.fillRect(x, y, 96, 5); // 总宽度96像素

        g.setColor(Color.GREEN);
        g.fillRect(x, y, (int)(96 * (health / 10.0)), 5);

        g.setColor(Color.WHITE);
        g.setFont(new Font("Arial", Font.BOLD, 10));
        g.drawString("Boss HP: " + health, x + 5, y + 15);
    }

    public void hit(TankPanel panel) {
        if (!isAlive) return;

        health--;
        System.out.println("Boss被击中！剩余生命值: " + health);

        if (health <= 0) {
            isAlive = false;
            System.out.println("Boss死亡！");
            panel.gameOver(false); // 游戏结束（玩家失败）
            //panel.stopAllThreads(); // 停止所有线程
        }
    }

    public boolean isAlive() {
        return isAlive;
    }
}